{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3f7a74b4-95ba-4e97-8aab-323ed4e00ec5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-15T00:36:11.742431Z",
     "iopub.status.busy": "2022-06-15T00:36:11.742006Z",
     "iopub.status.idle": "2022-06-15T00:36:12.345455Z",
     "shell.execute_reply": "2022-06-15T00:36:12.344810Z",
     "shell.execute_reply.started": "2022-06-15T00:36:11.742358Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "007fb16e-465b-4e86-9ca9-635251a3d5ca",
   "metadata": {},
   "source": [
    "Matplotlib没有专门的堆叠绘制api，可以通过plt.bar函数的bottom属性来实现堆叠绘制的效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d0613373-76f2-487d-bed9-85d809884319",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-15T00:36:12.347456Z",
     "iopub.status.busy": "2022-06-15T00:36:12.347183Z",
     "iopub.status.idle": "2022-06-15T00:36:12.352261Z",
     "shell.execute_reply": "2022-06-15T00:36:12.350764Z",
     "shell.execute_reply.started": "2022-06-15T00:36:12.347439Z"
    }
   },
   "outputs": [],
   "source": [
    "# 创建实验数据\n",
    "men = [20, 35, 32, 36, 23]\n",
    "women = [26, 30, 35, 22, 28]\n",
    "groups = [\"G1\", \"G2\", \"G3\", \"G4\", \"G5\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "338608ef-bf16-45a3-b7cc-8cf52bcf978d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-15T00:36:12.354132Z",
     "iopub.status.busy": "2022-06-15T00:36:12.353631Z",
     "iopub.status.idle": "2022-06-15T00:36:12.491047Z",
     "shell.execute_reply": "2022-06-15T00:36:12.490054Z",
     "shell.execute_reply.started": "2022-06-15T00:36:12.354115Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAApIAAAFSCAYAAABBiD9PAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAxOAAAMTgF/d4wjAAAYjElEQVR4nO3df7DdZX0n8PcnIQldorGkyWInDQhCWS0Wwq9mJ4KWCCqlbq2LSy1VYUatOO4s2w5rS3ejVoFqmU5b3aE7ugqVdQbp6rZb7RZcx6KCZAgGdNrE0jQGuzENVSd2JQl59o97b/aa5se5T05yzk1er5kz3PN8f33Ofbg37/t8v8/3W621AADATM0ZdQEAAMxOgiQAAF0ESQAAugiSAAB0ESQBAOgiSAIA0EWQBACgywmDrFRVz0nyuWlN/yzJ6UmWTu7jziRnJHk6yVtaaw8MtUoAAMbOQEGytfbtJOdOva+qX0lyaWvtqar6cJIHW2svr6oLk3yiqs5ore0+EgUDADAeBgqS+/HGJL8++fXVSZ6XJK21h6tqa5JV+cERzH9iwYIFbcmSJZ2HBwDgSHvyySd3ttYWHGj5jINkVa1MsjjJn1TV4iRzWmvbpq2yKcnyQ+1nyZIl2bJly0wPDwDAUVJV2w62vGeyzXVJ7px26nrfh3XXAQq5saq2TL127NjRcWgAAMbFjIJkVZ2U5LVJPpwkrbXtk+3Tz1GfmmTzvtu21m5vrS2bei1cuLC/agAARm6mI5L/Osn61tpfTmu7J8kNSTI52eaUJGZtAwAc42Z6jeT1ST60T9tNSe6qqo1Jdia51oxtAGAU9uzZk9b2veqOA6mqzJnTf1vxGQXJ1tqL99O2Ncnl3RUAABymnTt3ZvPmzdm1a9eoS5l15s2bl+XLl2f+/Pkz3rb39j8AAGNj8+bNedaznpXFixenar/zftmP1lq2b9+ezZs35/nPf/6MtxckAYBZbc+ePdm1a1cWL16cE04QbWZq8eLFeeqpp7Jnz54Zn+b2rG0AYFabuibSSGSfqe9bz7WlYjsAcMw57T/8zyO27023XnnE9j3bGJEEAKCLIAkAMGRVlVtuuSUXXXRRTj/99Nx33315xzvekfPOOy8vfOEL89WvfnXvunfddVcuvvjirFixIpdeemkef/zxJMlHPvKRXHHFFbnmmmtyzjnn5IILLsgTTzwxqo+0X4IkAMAR8OxnPztf/vKXc9ttt+VVr3pVVq1alXXr1uX1r3993vOe9yRJvvCFL+TjH/94Pv/5z+eRRx7Jb/7mb+Z1r3vd3n089NBDufXWW/PYY49l9erVue2220b1cfbLNZIAAEfAa1/72iTJihUrMmfOnFx55cS1leeff37+6I/+KEnyqU99Kl/5yldy8cUX791u27Zt2blzZ5Jk1apVOfXUU5MkK1euzO/93u8dzY9wSIIkMF7WLBp1BcenNd8ZdQVwzDnxxBOTJHPnzs2CBQv2ts+dOze7d088BLC1luuuuy7vete7DrqPfbcbF05tAwCMyFVXXZU777wz3/jGN5JM3BNz7dq1I65qcEYkAQBG5JJLLsl73/vevOpVr8ozzzyTXbt25corr8wFF1ww6tIGUqN6sPmyZcvali1bRnJsYIw5tT0aTm0ziz3zzDPZsGFDzjrrrMydO3fU5cw6B/v+VdWTrbVlB9rWqW0AALoIkgAAdBEkAQDoIkgCANDFrG1mNxMzRsPEDABiRBIAgE6CJAAAXZzaBgCOPUfy0ieX9+xlRBIAgC6CJADAkN1xxx1585vfnCRZv359qip//ud/niT5jd/4jbz73e/O2rVrs3LlyrzoRS/KRRddlC984QtJkk2bNuVHfuRHcvPNN+e8887L2WefnbVr1+ZNb3rT3nW/+c1v7j3W+9///lx00UVZsWJFXvnKV+59bveaNWvyC7/wC7nqqqvyghe8ID/90z+dp556aqifU5AEABiyl73sZXuD4/3335+VK1fm/vvvT5Lcd999ueSSS/LqV786a9asyfr163P77bfnNa95Tb73ve8lSbZv356VK1dm3bp1uf7667N69eq89a1vzfr163PBBRfk93//95Mkd999dzZs2JAvfelLeeSRR3LNNdfkbW972946HnrooXz0ox/N1772tSxdujR33HHHUD+nayQBAIbs9NNPT5I88cQTue+++3LLLbfkV3/1V/Pd7343GzZsyMknn5z58+fniiuuSJKsWrUqS5cuzfr16/Pc5z43CxcuzJVXXpkkWbFiRZYtW5Zzzz03SXL++efvDamf/OQns3bt2px//vlJJp6bPf152a94xSty8sknJ0lWrlyZxx57bKifU5AEADgCLrvssnz605/O17/+9Vx66aXZs2dP7r333qxatSqttVTVP9lmqm3BggV72+bOnZsTTzzxB97v3r07SdJay80335zrrrtuvzUcaLthcWobAOAIWL16dd73vvfl4osvTpK89KUvzTvf+c6sXr06Z599dp5++ul89rOfTZJ88YtfzLe+9a2cc845MzrGz/7sz+aDH/zg3msfd+3alXXr1g33gxyEEUkAgCPgsssuy+bNm7N69eokE9dNvv/978/q1aszf/783HvvvXn729+e733veznxxBNzzz335KSTTsq2bdsGPsa1116b7du35yUveUmqKrt3787111+f884770h9rB9QrbWjcqB9LVu2rG3ZsmUkx+YY4hGJo3Ek76GmT0fDffGYxZ555pls2LAhZ5111g9cH8hgDvb9q6onW2vLDrStU9sAAHQRJAEA6CJIAgDQRZAEAGa1qVvmjGrex2w39X3b3+2IDsWsbQBgVpszZ07mzZuX7du3Z/HixV2B6HjVWsv27dszb968zJkz8/FFQRIAmPWWL1+ezZs3D/1Z0seDefPmZfny5V3bDhwkq2pBkt9OckWSnUnWtdZ+saqWJrkzyRlJnk7yltbaA13VAAB0mD9/fp7//Odnz549TnHPQFV1jUROmcmI5K1J9iQ5q7XWquq509ofbK29vKouTPKJqjqjtTbcZ/AAABzC4YQiZm6gIFlVJyV5Y5JlbTLmt9b+bnLx1UmeN9n2cFVtTbIqyeeGXi0AAGNj0Nh+RpLtSW6uqrVV9RdVdVlVLU4yp7U2/Vk+m5L0nWgHAGDWGDRIzktyepKvtdYuSPK2JB/PxIjmvhci7HeqVFXdWFVbpl47duzorRkAgDEwaJD820xcH/mxJGmtfSXJ3yT5F0lSVUumrXtqks377qC1dntrbdnUa+HChYdVOAAAozVQkGyt/X2S+zMxYztVdWomrov8qyT3JLlhsv3CJKckMW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ki8l+clRFEiXg/Xrk0m+5o+CwyNIjomqOjPJzyX5san/2ZOktfbH01Z7oKpecpRLo9OAfTrV9v2qejTJ6UevQnrMpF+TzM/ECOWeo1QeHQbp06p6cZIzMzHbV5CcBQ7Vr/49HQ7XSI6P85J8vbX21KgLYWgG7tOqOiXJazJxmoXxdsh+rarTJv8w+PtM3Fv3D45SbfQ5aJ9W1UlJfifJLx/Nojhsg/wO/vGqeqSqHq6qtx6two4lguR42XttXFWdUVWPVtVfVdV/GWVRHJZD9mlVPTvJHyf5rdbaI6Mokhk7aL+21ja11s5NckqSBUlePZoymYGD9en7knygtfbk6Mqj08H69ZEky1prKzIxcvmWqrp6VIXOVoLk+FiX5Myq+uEkaa399eQ/RLck+eFRFka3Q/ZpVT0ryWeS/I/Wmhvkzg4D/6y21nYk+XiS1x3tIpmRQ/XpqiT/sao2ZaI/z6mqr46oVgZ30H5trX23tfadyWVbkvy3JC8eVbGzlSA5JlprGzMxa/dDVfWcaYtOGk1FHK5D9WlVLcxEiPyz1tq7j36F9BigX8+YnCmaqpqfidHI9Ue7TgZ3qD5trb2otXZaa+20JP8myWOttRce9UKZkQF+Vp87OSFu6o/6n8lE+GQGTLYZL29I8utJHqqqZ5L8Q5JvJbm1qhYk+etMnCZbVFVbktzVWnvHqIplIG/IAfo0yb9NclGSk6rq5ybXv6e19p5RFMqMvCEH7teXJPl3k+0nJPlsEn8ojL835MB9yuz1hhy4X38+yS9X1e5M/Kzek4lZ3syARyQCANDFqW0AALoIkgAAdBEkAQDoIkgCANBFkAQAoIsgCQBAF0ESAIAugiQAAF0ESQAAuvw/G6NiDUlWONoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 800x400 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 设置画板\n",
    "plt.figure(figsize=(10, 5), dpi=80)\n",
    "# 设置men数据的绘制\n",
    "plt.bar(groups, men, label=\"men\")\n",
    "# 设置women数据的绘制，以men数据为bottomm\n",
    "plt.bar(groups, women, bottom=men,label=\"women\")\n",
    "# 绘制图例\n",
    "plt.legend()\n",
    "# 直接绘图\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
